package main

import (
	"fmt"
	"sync"
)

var wg = sync.WaitGroup{}

func f1(chan1 chan string) {
	defer wg.Done()
	// defer close(chan1)
	for i := 1; i <= 10; i++ {
		chan1 <- "chan1 " + fmt.Sprintf("%d", i)

	}
}

func f2(chan2 chan string) {
	defer wg.Done()
	// defer close(chan2)
	for i := 1; i <= 10; i++ {
		chan2 <- "chan2 " + fmt.Sprintf("%d", i)
	}
}

func f3(chan1 chan string, chan2 chan string) {
	defer wg.Done()
	for {
		select {
		case x := <-chan1:
			fmt.Printf("%v ", x)
		case y := <-chan2:
			fmt.Printf("%v ", y)
		default:
			break
		}
	}

}

func main() {
	var chan1 chan string = make(chan string, 10)
	var chan2 chan string = make(chan string, 10)
	wg.Add(3)
	go f1(chan1)
	go f2(chan2)
	go f3(chan1, chan2)

	wg.Wait()
	fmt.Printf("结束\n")

}
